<?php 
namespace app\common\model;
class Jifen extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	protected function getAddtimeAttr($value, $data){
		return date('Y-m-d H:i', $value);
	}
	protected function getUsernameAttr($value, $data){
		return model('AdminUser')->where('userid',$data['userid'])->value('username');
	}
	protected function getTasknameAttr($value, $data){
		if ($data['typeid'] == 1) {
			$goodsname = model('Convert')->where('id', $data['contentid'])->value('title');
			return '兑换商品：'.$goodsname;
		}else{
			return config('TASKNAME.'.$data['taskid']);
		}
	}
	protected function getTypenameAttr($value, $data){
		return config('JIFEN_TYPE.'.$data['typeid']);
	}
	protected function getGettimeAttr($value, $data){
		return date('Y-m-d H:i:s', $data['addtime']);
	}
	protected function getAlldataAttr($value, $data){
		$data['nickname'] = model('AdminUser')->get_nickname($data['userid']);
		$data['adddate'] = $data['addtime'] ? date("Y-m-d H:i",$data['addtime']) : '未知';
		if ($data['contentid']) {
			$data['goodsname'] = model("Convert")->where('id', $data['contentid'])->value('title');
		}
		$data['typename'] = config('JIFEN_TYPE.'.$data['typeid']);
		$data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
        $data['dealer'] = model('AdminUser')->get_dealer($data['userid']);
		trace($data);
		return $data;
	}
	protected function getDealerAttr($value, $data){
        return model('AdminUser')->get_dealer($data['userid']);
    }
	//typeid 1:登录，2:介绍建档，3：成交，4：分享海报，5：分享文章，6：分享活动,7：兑换商品
	public function save_datas($userid, $typeid, $contentid=0){
		$count = $this->whereTime('addtime', 'today')->where('userid', $userid)->where('typeid', $typeid)->where('contentid', $contentid)->count();
		if ($count) {
			return true;
		}
		$addday = date('Y-m-d', SYS_TIME);
		$today_jifen = model('ShareStat')->where('userid', $userid)->where('typeid', 1)->where('kind', 'in', [4,5])->where('addday', $addday)->sum('nums');
		$max_jifen = config('SHARE_JIFEN_MAX');
		if ($today_jifen >= $max_jifen) {
			return true;
		}
		$jifen = config('JIFEN_NUMS.'.$typeid);
		$nums = ($max_jifen - $today_jifen) < $jifen ? ($max_jifen - $today_jifen) : $jifen;
		$data = [
			'userid' => $userid,
			'contentid' => $contentid,
			'typeid' => $typeid,
			'nums' => $nums,
		];
		$id = $this->save_data($data);
		if (!$id) {
			return false;
		}
		if ($typeid != 1) {
			model('ShareStat')->add_log($userid, $typeid);
		}
		model('ShareStat')->add_log($userid, 10, $nums);
		return model("AdminUser")->where('userid', $userid)->setInc('jifen', $nums);
	}
	//获取当日是否签到
	public function get_sign($userid){
		return $this->where('userid', $userid)->where('taskid', 1)->whereTime('addtime', 'today')->count();
	}
}